
local function print(message) DEFAULT_CHAT_FRAME:AddMessage("|cff33ff99Wheel:|r "..tostring(message)) end

local Wheel = Wheel

local new, del = Wheel.new, Wheel.del

local GUID = LibStub("LibGUIDRegistry-0.1")
local LSM = LibStub("LibSharedMedia-3.0")
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")

local LSM_Fonts = {}
local LSM_Textures = {}

local defaults = {
	profile = {
		enablezone = {
			world = false,
			bg = false,
			arena = false,
			partyinstance = false,
			raidinstance = true,
		},
		cooldownview = {
			width = 220,
			height = 25,
			scale = 1,
		},
		buffview = {
			width = 220,
			height = 25,
			scale = 1,
		},
--		testduration = 3,
	},
}

local function getGroupOption(info)
	local group = info[#info - 1]
	local option = info[#info]
	return Wheel.db.profile[group][option]
end

local function setGroupOption(info, v)
	local group = info[#info - 1]
	local option = info[#info]
	Wheel.db.profile[group][option] = v

	Wheel:ApplySettings(group)
end

local function addGroupOptions(group)
	local group = group

	local table = {}

	table.lock = {
		type = "toggle",
		name = "Show Anchor",
		desc = "Shows the bar anchor",
		get = getGroupOption,
		set = setGroupOption,
		order = 1,
	}

	table.width = {
		type = "range",
		name = "Bar Width",
		desc = "Bar Width",
		min = 20,
		max = 500,
		step = 5,
		get = getGroupOption,
		set = setGroupOption,
		order = 10,
	}

	table.height = {
		type = "range",
		name = "Bar Height",
		desc = "Bar Height",
		min = 5,
		max = 50,
		step = 1,
		get = getGroupOption,
		set = setGroupOption,
		order = 11,
	}

	table.scale = {
		type = "range",
		name = "Bar Scale",
		desc = "Bar Scale",
		min = 0.5,
		max = 5,
		step = 0.1,
		isPercent = true,
		get = getGroupOption,
		set = setGroupOption,
		order = 12,
	}

	table.growth = {
		type = "toggle",
		name = "Grow up",
		desc = "Set growth up",
		get = getGroupOption,
		set = setGroupOption,
		order = 13,
	}

	table.header1 = {
		type = "header",
		name = "",
		order = 20,
	}

	table.texture = {
		type = "select",
		name = "Texture",
		desc = "Set the texture for the timer bars",
		values = LSM_Textures,
		dialogControl = "LSM30_Statusbar",
		get = getGroupOption,
		set = setGroupOption,
		order = 21,
	}

	table.font = {
		type = "select",
		name = "Font",
		desc = "Set the font for the timer bars",
		values = LSM_Fonts,
		dialogControl = "LSM30_Font",
		get = getGroupOption,
		set = setGroupOption,
		order = 22,
	}

	table.fontsize = {
		type = "range",
		name = "Font Size",
		desc = "Set the bar font size",
		min = 7,
		max = 48,
		step = 1,
		get = getGroupOption,
		set = setGroupOption,
		order = 23,
	}

	table.header3 = {
		type = "header",
		name = "",
		order = 30,
	}

	table.flash = {
		type = "toggle",
		name = "Flash",
		desc = "Enables flashing bars",
		get = getGroupOption,
		set = setGroupOption,
		order = 32,
	}

	table.header2 = {
		type = "header",
		name = "",
		order = 40,
	}

	table.testrun = {
		type = "execute",
		name = "Test",
		desc = "Test bars",
		func = function()
			Wheel:Test(group)
		end,
		order = 3,
	}

	table.header4 = {
		type = "header",
		name = "",
		order = 5,
	}

	return table
end

local options = {
	type = "group",
	args = {
		spells = {
			type=  "group",
			name = "Spells",
			order = 10,
			args = {
			},
		},
		groups = {
			type = 'group',
			name = "Groups",
			order = 20,
			plugins = {},
			args = {
				new = {
					type = "input",
					name = "New group",
					order = 10,
					get = false,
					set = 'CreateGroup',
					pattern = '^%w+$',
				},
				delete = {
					type = 'select',
					name = "Delete group",
					order = 20,
					values = 'GetGroupList',
					confirm = function()
						return "Are you sure you want to delete this group"
					end,
					get = false,
					set = 'DeleteGroup',
					disabled = function() return not next(db.groups) end,
				},
				enable = {
					type = 'multiselect',
					name = "Groups",
					order = 30,
					values = 'GetGroupList',
					get = 'IsGroupEnabled',
					set = 'SetGroupEnabled',
				},
			},
		},
		enablezone = {
			type= "group",
			name = "Zone",
			desc = "Zone",
			order = 2,
			args = {
				world = {
					type = "toggle",
					name = "World",
					desc = "Enable reports in the outside world",
					get = function()
						return Wheel.db.profile.enablezone.world
					end,
					set = function(info, v)
						Wheel.db.profile.enablezone.world = v
						Wheel:CheckEnable()
					end,
					order = 1,
				},
				bg = {
					type = "toggle",
					name = "Battlegrounds",
					desc = "Enable reports in battlegrounds",
					get = function()
						return Wheel.db.profile.enablezone.bg
					end,
					set = function(info, v)
						Wheel.db.profile.enablezone.bg = v
						Wheel:CheckEnable()
					end,
					order = 2,
				},
				arena = {
					type = "toggle",
					name = "Arena",
					desc = "Enable reports in arena",
					get = function()
						return Wheel.db.profile.enablezone.arena
					end,
					set = function(info, v)
						Wheel.db.profile.enablezone.arena = v
						Wheel:CheckEnable()
					end,
					order = 3,
				},
				partyinstance = {
					type = "toggle",
					name = "Party Instances",
					desc = "Enable reports in 5-man instances",
					get = function()
						return Wheel.db.profile.enablezone.partyinstance
					end,
					set = function(info, v)
						Wheel.db.profile.enablezone.partyinstance = v
						Wheel:CheckEnable()
					end,
					order = 4,
				},
				raidinstance = {
					type = "toggle",
					name = "Raid Instances",
					desc = "Enable reports in raid instances",
					get = function()
						return Wheel.db.profile.enablezone.raidinstance
					end,
					set = function(info, v)
						Wheel.db.profile.enablezone.raidinstance = v
						Wheel:CheckEnable()
					end,
					order = 5,
				},
			},
		},
	},
}

local function addBarOptions()
	options.args.cooldownview = {
		type = "group",
		name = "Cooldowns",
		desc = "Cooldowns",
		args = addMenuOptions("cooldownview"),
	}

	options.args.buffview = {
		type = "group",
		name = "Buffs",
		desc = "Buffs",
		args = addMenuOptions("buffview"),
	}
end

